package com.eltonkent.dashboard.data.container;

import com.eltonkent.dashboard.util.MathUtil;
import com.eltonkent.dashboard.util.gps.GPSUtil;

/**
 * Contains weather information for the given location
 * <p>
 * Google Returns the following information
 * <ul>
 * <li>Temp in F
 * <li>Temp in C
 * <li>Humidity
 * <li>Wind Condition
 * <li>Weather Condition
 * </ul>
 * Yahoo Returns the following
 * <ul>
 * <li>Wind Condition
 * <li>Humidity
 * <li>Pressure
 * <li>Sunrise
 * <li>Sunset
 * <li>Temperature in F
 * <li>Weather Condition
 * </ul>
 * </p>
 * 
 * @author elton.stephen.kent
 * 
 */
public class WeatherContainer extends Container {
	private String tempInFaren;
	private String tempInCelcius;
	private String tempInKelvin;
	private String humidity;

	private String windCondition;
	private boolean isValidResponse = true;
	private String weatherCondition;
	private String pressure;

	private String sunrise;
	private String sunset;
	private boolean isGoogleResponse;

	private boolean isFarenheit;

	private String windScale;
	private String pressureScale;
	private String windDirection;

	public String getHumidity() {
		return humidity;
	}

	public String getPressure() {
		return pressure;
	}

	public String getSunrise() {
		return sunrise;
	}

	public String getSunset() {
		return sunset;
	}

	public String getTempInCelcius() {
		return tempInCelcius;
	}

	public String getTempInFaren() {
		return tempInFaren;
	}

	public String getTempInKelvin() {
		return tempInKelvin;
	}

	//
	// public String getWeatherCondition() {
	// return weatherCondition;
	// }

	public String getWindCondition() {
		return windCondition;
	}

	public boolean isGoogleResponse() {
		return isGoogleResponse;
	}

	public boolean isValidResponse() {
		return isValidResponse;
	}

	public void setGoogleHumidity(String humidity) {
		this.humidity = humidity;
		if (this.humidity.contains("Humidity:"))
			this.humidity = humidity.substring(humidity.indexOf(':') + 1);
		this.humidity = this.humidity.replace('%', ' ');
		this.humidity=this.humidity.trim();
	}

	public void setGoogleResponse(boolean isGoogleResponse) {
		this.isGoogleResponse = isGoogleResponse;
	}

	public void setPressure(String pressure) {
		this.pressure = pressure;
	}

	public void setPressureScale(String scale) {
		this.pressureScale = scale;
	}

	public String getPressureScale() {
		return pressureScale;
	}

	public void setSunrise(String sunrise) {
		this.sunrise = sunrise;
	}

	public void setSunset(String sunset) {
		this.sunset = sunset;
	}

	public void setTempInCelcius(String tempInCelcius) {
		this.tempInCelcius = tempInCelcius;
	}

	public void setTempInFaren(String tempInFaren) {
		this.tempInFaren = tempInFaren;
	}

	public void setTempScale(String scale) {
		if (scale.equals("F")) {
			isFarenheit = true;
		}
	}

	public void setValidResponse(boolean isValidResponse) {
		this.isValidResponse = isValidResponse;
	}

	private int weatherstatus;

	public void setWeatherCondition(String weatherCondition) {
		this.weatherCondition = weatherCondition;
		this.weatherCondition = this.weatherCondition.toLowerCase();

		if (this.weatherCondition.contains("hot")
				|| this.weatherCondition.contains("sun")) {
			weatherstatus = 1;
		} else if (weatherCondition.contains("snow")) {
			weatherstatus = 2;
		} else if (weatherCondition.contains("cloudy")) {
			weatherstatus = 3;
		} else if (weatherCondition.contains("rain")
				|| weatherCondition.contains("showers")) {
			weatherstatus = 4;
		} else if (weatherCondition.contains("fog")) {
			weatherstatus = 5;
		} else if (this.weatherCondition.contains("partly cloudy")) {
			weatherstatus = 6;
		}

	}

	public int getWeatherCondition() {
		return weatherstatus;
	}

	public void setGoogleWindCondition(String windCondition) {
		this.windCondition=windCondition;
		StringBuilder windData = new StringBuilder(windCondition);

		if (this.windCondition.contains("Wind:")) {
			this.windCondition = windCondition.substring(windCondition
					.indexOf(':') + 1);
			
			windData.delete(0,windCondition.indexOf(':') + 1);
		}
		
		if(windCondition.endsWith("mph")){
			
			windData.delete(windData.length()-4, windData.length());
			windScale="mph";
		}
		
		
//		if (windCondition.endsWith("mph") || windCondition.endsWith("MPH")) {
//			this.windCondition = this.windCondition.substring(0,
//					this.windCondition.indexOf("MPH"));
//			this.windScale = "mph";
//		} else if (windCondition.endsWith("KMPH")
//				|| windCondition.endsWith("KM/H")) {
//			this.windCondition = this.windCondition.substring(0,
//					this.windCondition.indexOf("KMPH"));
//			this.windScale = "KMPH";
//		}
		
		this.windCondition=windData.toString();
	}

	public void setWindScale(String scale) {
		this.windScale = scale;
	}

	public String getWindScale() {
		return windScale;
	}

	public void setYahooHumidity(String humidity) {
		this.humidity = humidity;
	}

	public void setYahooTemperature(String temperature) {
		try {
			float neutralTemp = Float.valueOf(temperature);
			if (isFarenheit) {
				tempInFaren = temperature;
				float kelvinTemp = neutralTemp - 32;
				kelvinTemp = kelvinTemp / 1.8f;
				kelvinTemp += 273.15;
				tempInKelvin = String.valueOf(kelvinTemp);
				neutralTemp -= 32;
				neutralTemp = neutralTemp / 1.8f;
				neutralTemp = (float) MathUtil.round(neutralTemp, 2);
				tempInCelcius = String.valueOf(neutralTemp);
			} else {
				tempInCelcius = temperature;
				tempInKelvin = String.valueOf((neutralTemp + 273.15));
				neutralTemp = neutralTemp * 9;
				neutralTemp = neutralTemp / 5;
				neutralTemp += 32;
				neutralTemp = (float) MathUtil.round(neutralTemp, 2);
				tempInFaren = String.valueOf(neutralTemp);
			}
		} catch (NumberFormatException e) {
			tempInFaren = temperature;
			tempInCelcius = temperature;
			e.printStackTrace();
		}
	}

	public void setYahooWindCondition(String windCondition) {
		this.windCondition = windDirection + " at " + windCondition;
	}

	public void setYahooWindDirection(String direction) {
		try {
			this.windDirection = GPSUtil.getWindDirection(Float
					.valueOf(direction));
		} catch (NumberFormatException e) {
			windDirection = "";
		}
	}

	@Override
	public String toString() {
		return "WeatherContainer [humidity=" + humidity + ", pressure="
				+ pressure + ", pressureScale=" + pressureScale + ", sunrise="
				+ sunrise + ", sunset=" + sunset + ", tempInCelcius="
				+ tempInCelcius + ", tempInFaren=" + tempInFaren
				+ ", tempInKelvin=" + tempInKelvin + ", weatherCondition="
				+ weatherCondition + ", weatherstatus=" + weatherstatus
				+ ", windCondition=" + windCondition + ", windScale="
				+ windScale + "]";
	}

}
